package com.bjsxt.service.impl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.domain.Scheduling;
import com.bjsxt.dto.SchedulingFormDto;
import com.bjsxt.dto.SchedulingQueryDto;
import com.bjsxt.mapper.SchedulingMapper;
import com.bjsxt.service.SchedulingService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class SchedulingServiceImpl implements SchedulingService{

    @Autowired
    private SchedulingMapper schedulingMapper;

    /**
     * 查询医生的排班信息
     * @param schedulingQueryDto
     * @return
     */
    @Override
    public List<Scheduling> queryScheduling(SchedulingQueryDto schedulingQueryDto) {
        QueryWrapper<Scheduling> qw = new QueryWrapper<>();
        // 根据用户id查询
        qw.eq(null!=schedulingQueryDto.getUserId(),Scheduling.COL_USER_ID,schedulingQueryDto.getUserId());
        // 根据科室id查询
        qw.eq(null!=schedulingQueryDto.getDeptId(),Scheduling.COL_DEPT_ID,schedulingQueryDto.getDeptId());
        // 根据排班日期查询
        qw.ge(Scheduling.COL_SCHEDULING_DAY,schedulingQueryDto.getBeginDate());
        qw.le(Scheduling.COL_SCHEDULING_DAY,schedulingQueryDto.getEndDate());
        return this.schedulingMapper.selectList(qw);
    }

    /**
     * 修改医生的排班信息 只修改医生修改这个周的信息 其他周信息不修改
     * 此处修改医生排班信息的整体逻辑是：
     * TODO 先删除该医生之前的排班信息
     *      然后再将新的排班信息重新插入
     * @param schedulingFormDto
     * @return
     */
    @Override
    public int saveScheduling(SchedulingFormDto schedulingFormDto) {
        // 校验参数
        if (null!=schedulingFormDto.getData()&&schedulingFormDto.getData().size()>0){
            DateTime dateTime = DateUtil.parse(schedulingFormDto.getBeginDate(), "yyyy-MM-dd");
            DateTime date = DateUtil.beginOfWeek(dateTime);
            // 获取当前修改排班周的开始日期 也就是周一
            String beginDate = DateUtil.format(date, "yyyy-MM-dd");
            // 获取当前修改排班周的结束日期 也就是周日
            String endDate = DateUtil.format(DateUtil.endOfWeek(date), "yyyy-MM-dd");
            // 因为修改医生的排班信息每次只能修改一个 即每次传来集合中的医生id和科室id都是一样的
            // 所以我们只需从前端传过来的医生排班信息集合中获取一个的医生信息即可
            SchedulingFormDto.SchedulingData schedulingData = schedulingFormDto.getData().get(0);
            //获取医生id
            Long userId = schedulingData.getUserId();
            // 获取科室id
            Long deptId = schedulingData.getDeptId();

            if (null!=userId){
                // 删除医生原来的排班信息
                QueryWrapper<Scheduling> qw = new QueryWrapper<>();
                qw.eq(Scheduling.COL_USER_ID,userId);
                qw.eq(Scheduling.COL_DEPT_ID,deptId);
                // 删除修改的这个周的医生信息
                qw.between(Scheduling.COL_SCHEDULING_DAY,beginDate,endDate);
                // 删除
                this.schedulingMapper.delete(qw);

                // 插入新的排班信息
                // 初始化修改周的7天日期
                List<String> schedulingDays = initSchedulingDay(date);
                // 遍历用户传过来的早上、中午、晚上的排班数据
                for (SchedulingFormDto.SchedulingData datum : schedulingFormDto.getData()) {
                    // 封装插入数据
                    Scheduling scheduling;
                    int i =0;
                    // 遍历用户一周七天的排班类型
                    for (String s : datum.getSchedulingType()) {
                        if (StringUtils.isNotBlank(s)){
                            scheduling = new Scheduling(userId,deptId,schedulingDays.get(i),datum.getSubsectionType(),s,new Date(),schedulingFormDto.getSimpleUser().getUserName());
                            this.schedulingMapper.insert(scheduling);
                        }
                        i++;
                    }
                }
                return 1;
            }else {
                return 0;
            }
        }else {
            return 0;
        }
    }

    /**
     * 根据条件查询出有号的科室id集合
     * @param deptId
     * @param schedulingDay
     * @param schedulingType
     * @param subsectionType
     * @return
     */
    @Override
    public List<Long> queryHasSchedulingDeptIds(Long deptId, String schedulingDay, String schedulingType, String subsectionType) {
        return schedulingMapper.queryHasSchedulingDeptIds(deptId,schedulingDay,schedulingType,subsectionType);
    }

    /**
     * 初始化修改周的7天日期
     * @param date
     * @return
     */
    private List<String> initSchedulingDay(DateTime date) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            DateTime dateTime = DateUtil.offsetDay(date, i);
            // 格式化
            String format = DateUtil.format(dateTime, "yyyy-MM-dd");
            list.add(format);
        }
        return list;
    }
}
